<?php
include '../lib/BLL/TrungTamBLL.php';
include '../lib/BLL/DmMonHocBLL.php';
include '../lib/BLL/DonViHocVuBLL.php';
include '../lib/BLL/SinhVienBLL.php';
include '../lib/BLL/DmCaHocBLL.php';
include '../lib/BLL/HoiDongThiBLL.php';

$madsthi = $_GET['dsthi'];


$hoidong = new HoiDongThiBLL();
//$hd = $hoidong->getHoiDong_indirect($ma_donvi, $hocvu);
//$smarty->assign('hd',$hd);

$dv = new TrungTamBLL();
$mh = new DmMonHocBLL();
$dvhv = new DonViHocVuBLL();
$sv = new SinhVienBLL();
$cathi = new DmCaHocBLL();
$dsthi = $db->getOneRow("SELECT dst.ma_hoidongthi, tt.ten_trungtam, mh.madm_mh, tt.ma_trungtam, mh.ten_mh, dvhv.ma_donvihocvu, dst.ngaythi, dst.madm_ca, dst.trangthai_dsthi FROM tbl_dsthi AS dst
INNER JOIN tbl_hoidongthi AS hdt ON hdt.ma_hoidong = dst.ma_hoidongthi
INNER JOIN tbl_trungtam AS tt ON tt.ma_trungtam = hdt.ma_trungtam
INNER JOIN dm_monhoc AS mh ON mh.madm_mh = dst.ma_mon_ctdt
INNER JOIN tbl_donvihocvu AS dvhv ON hdt.ma_donvihocvu = dvhv.ma_donvihocvu WHERE dst.ma_dsthi = '$madsthi'");

$donvi = $dsthi['ten_trungtam'];
$madonvi = $dsthi['ma_trungtam'];
$monhoc = $dsthi['ten_mh'];
$mamonhoc = $dsthi['madm_mh'];
$hocvu = $dsthi['ma_donvihocvu'];
$sods = $dsthi['trangthai_dsthi'];
$mahdthi = $dsthi['ma_hoidongthi'];
$macathi = $dsthi['madm_ca'];
$ngaythi = date('Y-m-d', $dsthi['ngaythi']);
/*
$listdkm = $db->getList("SELECT ma_dkm FROM tbl_diemthi WHERE ma_dsthi = '$madsthi'");
$dkmarr = array();
foreach($listdkm as $dkm){
	$dkmarr[] = $dkm['ma_dkm'];
}
$dkmarr = implode(",", $dkmarr);
if(empty($dkmarr)) $dkmarr = 0;
 /*AND dkm.ma_dkm NOT IN(
SELECT ma_dkm FROM tbl_diemthi WHERE ma_dsthi =  '113AAB10801')
SELECT sv.hoten_sv FROM tbl_sinhvien AS sv INNER JOIN tbl_dangkymon AS dkm ON dkm.ma_sv = sv.ma_sv
WHERE dkm.ma_dkm IN(SELECT CONCAT(ma_dkm) FROM tbl_diemthi WHERE ma_dsthi = '113AAB10801')*/
//Get List SV
$sql = "SELECT dkm.ma_sv, dkm.ma_dkm, sv.hoten_sv, sv.ngaysinh_sv, dkm.ma_lopmon FROM tbl_dangkymon AS dkm
INNER JOIN tbl_sinhvien AS sv ON sv.ma_sv = dkm.ma_sv
WHERE ma_lopmon IN(SELECT lm.ma_lopmon FROM tbl_lopmon AS lm
INNER JOIN tbl_lopmon_monctdt AS lmctdt ON lm.ma_lopmon = lmctdt.ma_lopmon
INNER JOIN  tbl_mon_ctdt AS mctdt ON lmctdt.ma_mon_ctdt = mctdt.ma_mon_ctdt
INNER JOIN dm_monhoc AS mh ON mctdt.ma_monhoc = mh.madm_mh
WHERE lm.ma_trungtam = '$madonvi' AND lm.ma_donvihocvu = '$hocvu'
AND mh.madm_mh = '$mamonhoc'
AND dkm.ma_dkm NOT IN(SELECT ma_dkm FROM tbl_diemthi WHERE ma_dsthi IN(SELECT DISTINCT ma_dsthi FROM `tbl_dsthi` WHERE ma_hoidongthi = '$mahdthi' AND ma_mon_ctdt = '$mamonhoc') ))";
$svarr = $db->getList($sql);

$svarr2 = $db->getList("SELECT dkm.ma_sv, dkm.ma_dkm, sv.hoten_sv, sv.ngaysinh_sv, dkm.ma_lopmon FROM tbl_dangkymon AS dkm
INNER JOIN tbl_sinhvien AS sv ON sv.ma_sv = dkm.ma_sv
WHERE dkm.ma_dkm IN(SELECT ma_dkm FROM tbl_diemthi WHERE ma_dsthi = '$madsthi')");

$ds_sv = $sv->getListSinhvien();
$ds_cathi = $cathi->getListDmCaHoc();

$smarty->assign('monhoc',$monhoc);
$smarty->assign('donvi',$donvi);
$smarty->assign('hocvu',$hocvu);
$smarty->assign('hdthi',$mahdthi);
$smarty->assign('sods',$sods);
$smarty->assign('madsthi',$madsthi);

$smarty->assign('macathi', $macathi);
$smarty->assign('ds_sv',$ds_sv);
$smarty->assign('ds_cathi',$ds_cathi);
$smarty->assign('date',$ngaythi);
$smarty->assign('sinhvien',$svarr);
$smarty->assign('cursinhvien', $svarr2);
$smarty->display('tochucthi/suadsthi.tpl');
?>